﻿using System;
using System.Windows.Forms;

namespace Group_Mail
{
    public partial class frmAccountCreator : Form
    {
        public frmAccountCreator()
        {
            InitializeComponent();
        }

        public frmAccountCreator(Account account) : this()
        {
            Account = account;
        }

        public Account Account { get; set; }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (Account == null)
            {
                Account = new Account();
            }

            Account.Name = txtAccountName.Text.Trim();
            if (string.IsNullOrEmpty(Account.Name))
            {
                MessageBox.Show("Please enter your account name", "Account Creator", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                txtAccountName.Focus();
                return;
            }

            Account.FullName = txtFullName.Text.Trim();
            Account.Email = txtEmail.Text.Trim();

            if (!Utils.IsValidEmail(Account.Email))
            {
                MessageBox.Show("Please enter correct your email", "Account Creator", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }

            Account.ReplyTo = txtReplyTo.Text.Trim();
            Account.SmtpServer = txtSmtpServer.Text.Trim();

            if (string.IsNullOrEmpty(Account.SmtpServer))
            {
                MessageBox.Show("Please enter smtp server address", "Account Creator", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                txtSmtpServer.Focus();
                return;
            }

            Account.SmtpServerPort = Convert.ToInt32(txtPort.Text.Trim());
            Account.SmtpServerUser = txtUserName.Text.Trim();
            Account.SmtpServerPassword = txtPassword.Text.Trim();
            Account.EnableSsl = cbEnableSSL.Checked;

            if (string.IsNullOrEmpty(Account.Id))
            {
                Account.Id = Guid.NewGuid().ToString();
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void frmAccountCreator_Load(object sender, EventArgs e)
        {
            if (Account != null)
            {
                txtAccountName.Text = Account.Name;
                txtFullName.Text = Account.FullName;
                txtEmail.Text = Account.Email;
                txtReplyTo.Text = Account.ReplyTo;
                txtSmtpServer.Text = Account.SmtpServer;
                txtPort.Text = Account.SmtpServerPort.ToString();
                txtUserName.Text = Account.SmtpServerUser;
                txtPassword.Text = Account.SmtpServerPassword;
                cbEnableSSL.Checked = Account.EnableSsl;
            }
        }
    }
}